package org.awenlice.playerdailyshop.coin;

import com.mojang.logging.LogUtils;
import io.github.lightman314.lightmanscurrency.api.money.value.builtin.CoinValue;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import io.github.lightman314.lightmanscurrency.api.money.MoneyAPI;
import io.github.lightman314.lightmanscurrency.api.money.value.MoneyValue;
import org.slf4j.Logger;

public class PlayerCoinSystem {
    private static final String COIN_KEY = "player_coins";
    private static final Logger LOGGER = LogUtils.getLogger();

    // 使用中文
    private static String getMoneyCostString(MoneyValue moneyCost) {

        String moneyCostString = moneyCost.getString();

        moneyCostString = moneyCostString.replace("c", "铜币 ")
                .replace("i", "铁币 ")
                .replace("g", "金币 ")
                .replace("e", "绿宝石币 ")
                .replace("d", "钻石币 ")
                .replace("n", "下界合金币 ");

        return moneyCostString;
    }

    // 增加玩家的金币
    public static void addCoins(Player player, int amount) {

        MoneyValue moneyCost = CoinValue.fromNumber("main", amount);
        MoneyAPI.API.GetPlayersMoneyHandler(player).insertMoney(moneyCost, false);
        String moneyCostString = getMoneyCostString(moneyCost);
        // 局内提示
        player.displayClientMessage(Component.translatable("pds.addCoins", moneyCostString), false);
    }

    // 计算费用并判断余额是否足以支付费用
    public static boolean hasEnoughCoins(Player player, int amount) {
        TeleportCostVo teleportCostVo = CostUtil.TeleportCostCalculate(player, amount);

        // 扣费
        MoneyAPI.API.GetPlayersMoneyHandler(player).extractMoney(teleportCostVo.getCost(), false);

        // 判断是否强制使用中文
        String moneyCostString = getMoneyCostString(teleportCostVo.getCost());

        // 局内提示
        Component message;
        if (teleportCostVo.isCanAfford()) {
            player.displayClientMessage(Component.translatable("pds.alertMoneyCost", moneyCostString), false);
        } else {
            player.displayClientMessage(Component.translatable("pds.hasNotMoney"), false);
        }

        return teleportCostVo.isCanAfford();
    }


}